package com.uphone.kingmall.activity.personal;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.SearchGoodsListActivity;
import com.uphone.kingmall.adapter.CoolCommonRecycleviewAdapter;
import com.uphone.kingmall.adapter.CoolRecycleViewHolder;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.GetShopStatusBean;
import com.uphone.kingmall.bean.GetVipCenterInfoBean;
import com.uphone.kingmall.bean.WalletRecoedBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.view.CoolCustomRoundAngleImageView;

import org.json.JSONObject;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipCenterActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.linear_title)
    LinearLayout linearTitle;
    @BindView(R.id.img_head)
    CoolCustomRoundAngleImageView imgHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_out_time)
    TextView tvOutTime;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    @BindView(R.id.tv_value_money)
    TextView tvValueMoney;
    @BindView(R.id.tv_upgrade)
    TextView tvUpgrade;
    @BindView(R.id.tv_push)
    TextView tvPush;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.tv_award_money)
    TextView tvAwardMoney;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.rcv)
    RecyclerView rcv;

    private CoolCommonRecycleviewAdapter<WalletRecoedBean.DataBean> adapter;
    private List<WalletRecoedBean.DataBean> mDatas;
    private LinearLayoutManager layoutManager;
    private Double wallet = 0.0;

    private GetVipCenterInfoBean getVipCenterInfoBean;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_vip_center;
    }

    @Override
    public void initView() {
        mImmersionBar
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.status_bar)
                .navigationBarColor(R.color.black)
                .fullScreen(false)
                .init();


        layoutManager = new LinearLayoutManager(this);
        rcv.setLayoutManager(layoutManager);
        adapter = new CoolCommonRecycleviewAdapter<WalletRecoedBean.DataBean>(VipCenterActivity.this, mDatas, R.layout.kingmall_item_vip_center) {
            @Override
            protected void onBindView(CoolRecycleViewHolder holder, int position) {
                CoolCustomRoundAngleImageView img_head = holder.getView(R.id.img_head);
                TextView tv_nickname = holder.getView(R.id.tv_nickname);
                TextView tv_award_money = holder.getView(R.id.tv_award_money);
                TextView tv_phone = holder.getView(R.id.tv_phone);
                TextView tv_type = holder.getView(R.id.tv_type);
                GlideUtils.getInstance().loadCircleImage(VipCenterActivity.this, mDatas.get(position).getUserPhoto(), img_head);
                tv_nickname.setText(mDatas.get(position).getUserName());
//                tv_award_money.setText("+ " + mDatas.get(position).getMoney());
                tv_phone.setText(mDatas.get(position).getCreateTime());
                Map<Integer, String> walletType = ConstansUtils.getWalletType();
                String typevalue = walletType.get(mDatas.get(position).getResumeType());
                if (!TextUtils.isEmpty(typevalue)) {
                    tv_type.setText("" + typevalue);
                } else {
                    tv_type.setText("");
                }
                if (mDatas.get(position).getMoney() >= 0) {
                    tv_award_money.setText(String.format(Locale.CHINA, "+%.2f", mDatas.get(position).getMoney()));
                } else {
                    tv_award_money.setText(String.format(Locale.CHINA, "%.2f", mDatas.get(position).getMoney()));
                }
            }
        };
        rcv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        OkGoUtils.normalRequestGet(MyUrl.GetVipInfo, new onNormalRequestListener() {
            @Override
            public void onSuccess(String json, int code) {
                if (code == 0) {
                    getVipCenterInfoBean = GsonUtils.getGson().fromJson(json, GetVipCenterInfoBean.class);
                    tvName.setText(getVipCenterInfoBean.getData().getUserName());
                    GlideUtils.getInstance().loadCircleImage(VipCenterActivity.this, getVipCenterInfoBean.getData().getUserPhoto(), imgHead);
                    tvOutTime.setText("VIP会员将在" + getVipCenterInfoBean.getData().getExpireDate() + "过期");
                    tvValueMoney.setText("（已为你节省" + getVipCenterInfoBean.getData().getTotalSave() + "元）");
                    if (getVipCenterInfoBean.getData().getMemberType() == 2) {
                        tvUpgrade.setVisibility(View.VISIBLE);
                    } else {
                        OkGoUtils.normalRequestGet(MyUrl.GetShopStatus, new onNormalRequestListener() {
                            @Override
                            public void onSuccess(String json, int code) {
                                if (code == 0) {
                                    GetShopStatusBean bean = GsonUtils.getGson().fromJson(json, GetShopStatusBean.class);
                                    if (bean.getCode() == 0) {
                                        if (bean.getData().getShopStatus() == 1) {
                                            tvUpgrade.setVisibility(View.GONE);
                                        } else {
                                            tvUpgrade.setVisibility(View.VISIBLE);
                                        }
                                    } else {
                                        tvUpgrade.setVisibility(View.VISIBLE);
                                    }

                                }
                            }

                            @Override
                            public void onError(Response<String> response) {

                            }
                        });
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });

        OkGoUtils.normalRequest(MyUrl.getUserLingQianInfo, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    try {
                        if (code == 0) {
                            JSONObject jsonObject = new JSONObject(body);
                            JSONObject object = jsonObject.getJSONObject("lingqian");
                            wallet = object.getDouble("lingqian");
                            String total = object.getString("leiji");
                            tvAwardMoney.setText("（合计：" + total + "元）");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });


        HttpParams params = new HttpParams();
        params.put("page", 1);
        params.put("limit", 50);
        OkGoUtils.normalRequest(MyUrl.getUserShareRecoed, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String json, int code) {
                if (code == 0) {
                    WalletRecoedBean bean = GsonUtils.getGson().fromJson(json, WalletRecoedBean.class);
                    mDatas = bean.getData();
                    adapter.setmDatas(mDatas);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_pay, R.id.tv_upgrade, R.id.tv_push, R.id.tv_invite, R.id.tv_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_pay:
//                if (getVipCenterInfoBean.getData().getWay() == 1) {
//                    openActivity(VipBuyActivity.class);
//                } else {
                openActivity(VipContinueActivity.class);
//                }
                break;
            case R.id.tv_upgrade:
                GetShopStatus();
                break;
            case R.id.tv_push:
                openActivity(SearchGoodsListActivity.class);
                finish();
                break;
            case R.id.tv_invite:
                openActivity(InviteCodeActivity.class);
                break;
            case R.id.tv_withdraw:
                openActivity(SmallChargeActivity.class);
                break;
        }
    }

    private void GetShopStatus() {
        OkGoUtils.normalRequestGet(MyUrl.GetShopStatus, new onNormalRequestListener() {
            @Override
            public void onSuccess(String json, int code) {
                if (code == 0) {
                    GetShopStatusBean bean = GsonUtils.getGson().fromJson(json, GetShopStatusBean.class);
                    if (bean.getCode() == 0) {
                        if (bean.getData().getShopStatus() == 1) {

                        } else if (bean.getData().getShopStatus() == 4) {
                            if (getVipCenterInfoBean.getData().getMemberType() == 2) {
                                if (getVipCenterInfoBean.getData().getWay() == 1) {
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("type", 3);
                                    openActivity(VipUpgradeActivity.class, bundle);
                                } else {
                                    openActivity(VipUpgradeActivity.class);
                                }
                            } else {
                                openActivity(ShangJiaRuZhuActivity.class);
                            }
                        } else {
                            openActivity(ShangJiaRuZhuActivity.class);
                        }
                    } else if (bean.getCode() == 101 || bean.getCode() == 102) {
                        if (getVipCenterInfoBean.getData().getWay() == 0) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("type", 3);
                            openActivity(VipUpgradeActivity.class, bundle);
                        } else if (getVipCenterInfoBean.getData().getWay() == 1) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("type", 3);
                            openActivity(VipUpgradeActivity.class, bundle);
                        } else {
                            openActivity(VipUpgradeActivity.class);
                        }
                    }

                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }
}
